//
//  YDAlertShow.m
//  YDAlertShow
//
//  Created by James on 16/5/8.
//  Copyright © 2016年 bearbaby. All rights reserved.
//

#import "YDAlertShow.h"
#import "YDAlertView.h"
#import "YDAlertButton.h"
@interface YDAlertShow()
@property(nonatomic,strong)UIWindow *window;//因为提示框要加到整个window上，所以先定义一个window
@property(nonatomic,strong)UIView *backView;//背景半透明框
@property(nonatomic,strong)YDAlertView *ydView;//呈现内容的框
@property(nonatomic,strong)UILabel *titleLable;//标题
@property(nonatomic,strong)UILabel *contentLabel;//内容
@property(nonatomic,strong)YDAlertButton *confirmBtn;//确认按钮
@property(nonatomic,strong)YDAlertButton *cancleBtn;

@property(nonatomic,strong)UIImageView *iconImage;
@property(nonatomic,strong)UILabel *iconContent;

@end

@implementation YDAlertShow
+(instancetype)shareAlert{

    static YDAlertShow *alert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alert = [[YDAlertShow alloc]init];
    });
    return alert;
}
#pragma mark -- 提示 内容 按钮
+(void)showAlertWithTitle:(NSString *)ydTitle withContent:(NSString *)ydContent withActions:(NSArray *)ydActions{

    [[YDAlertShow shareAlert]showAlertWithTitle:ydTitle withContent:ydContent withActions:ydActions];
}
-(void)showAlertWithTitle:(NSString *)ydTitle withContent:(NSString *)ydContent withActions:(NSArray *)ydActions{

    [self.window addSubview:self.backView];
    [self.backView addSubview:self.ydView];
   [self.ydView.titleView addSubview:self.titleLable];
    [self.ydView.contentView addSubview:self.contentLabel];
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",ydTitle];
    self.contentLabel.text = [NSString stringWithFormat:@"%@",ydContent];
    for (int i = 0; i<ydActions.count; i++) {
        YDAlertButton *button = [[YDAlertButton alloc]initWithFrame:CGRectMake(self.ydView.width/2*i, 0, self.ydView.width/2, self.ydView.buttonView.height)];
       [self.ydView.buttonView addSubview:button];
        button.handle = ydActions[i];
        if(i==0)[button setTitle:@"取消" forState:(UIControlStateNormal)];
        if (i==1) {
            [button setTitle:@"确定" forState:(UIControlStateNormal)];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, self.ydView.buttonView.height)];
            lineView.backgroundColor = [UIColor grayColor];
            lineView.centerX = self.ydView.width/2;
            [self.ydView.buttonView addSubview:lineView];
        }
        UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.ydView.width, 1)];
        lineview.backgroundColor = [UIColor grayColor];
        [self.ydView.buttonView addSubview:lineview];
    }
    self.ydView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.ydView.alpha = 1;
    }];
////    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////        [self.backView removeFromSuperview];
////    });
}
#pragma mark -- 表情内容提示框
+(void)showAlertWithTitle:(NSString *)ydTitle withIconStatue:(YDIconStatue)iconStatue withContent:(NSString *)ydContent withButtonTitles:(NSArray *)buttonTitles withButtonAction:(NSArray<void (^)()> *)buttonActions{

    return [[YDAlertShow shareAlert]showAlertWithTitle:ydTitle withIconStatue:iconStatue withContent:ydContent withButtonTitles:buttonTitles withButtonAction:buttonActions];
}
-(void)showAlertWithTitle:(NSString *)ydTitle withIconStatue:(YDIconStatue)iconStatue withContent:(NSString *)ydContent withButtonTitles:(NSArray *)buttonTitles withButtonAction:(NSArray<void (^)()> *)buttonActions{

    [self.window addSubview:self.backView];
    [self.backView addSubview:self.ydView];
    [self.ydView.titleView addSubview:self.titleLable];
    [self.ydView.contentView addSubview:self.iconImage];
    [self.ydView.contentView addSubview:self.iconContent];
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",ydTitle];
    
    NSString *iconStr = [NSString stringWithFormat:@"ystatue%ld",iconStatue];
    NSLog(@"iconStr:%@",iconStr);
    self.iconImage.image = [UIImage imageNamed:iconStr];
    
    self.iconContent.text = [NSString stringWithFormat:@"%@",ydContent];
    
    for (int i = 0; i<buttonActions.count; i++) {
        YDAlertButton *button = [[YDAlertButton alloc]initWithFrame:CGRectMake(self.ydView.width/2*i, 0, self.ydView.width/2, self.ydView.buttonView.height)];
        [self.ydView.buttonView addSubview:button];
        button.handle = buttonActions[i];
        if(i==0)[button setTitle:buttonTitles[i] forState:(UIControlStateNormal)];
        if (i==1) {
            [button setTitle:buttonTitles[i] forState:(UIControlStateNormal)];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, self.ydView.buttonView.height)];
            lineView.backgroundColor = [UIColor grayColor];
            lineView.centerX = self.ydView.width/2;
            [self.ydView.buttonView addSubview:lineView];
        }
        UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.ydView.width, 1)];
        lineview.backgroundColor = [UIColor grayColor];
        [self.ydView.buttonView addSubview:lineview];
    }
    self.ydView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.ydView.alpha = 1;
    }];

}
#pragma mark -- 带输入框
-(instancetype)initWithTitle:(NSString *)ydTitle withPlaceholder:(NSString *)ydHolder withButtonTitles:(NSArray *)buttonTitles withConfirmHandle:(void (^)())confirmAction{

    self = [super init];
    if (self) {
        
        [self.window addSubview:self.backView];
        [self.backView addSubview:self.ydView];
        [self.ydView.titleView addSubview:self.titleLable];
        [self.ydView.contentView addSubview:self.inputField];
        
        self.titleLable.text = ydTitle;
        self.inputField.placeholder = ydHolder;
        
        for (int i = 0; i<buttonTitles.count; i++) {
            YDAlertButton *button = [[YDAlertButton alloc]initWithFrame:CGRectMake(self.ydView.width/2*i, 0, self.ydView.width/2, self.ydView.buttonView.height)];
            [self.ydView.buttonView addSubview:button];
            if(i==0){
                [button setTitle:buttonTitles[i] forState:(UIControlStateNormal)];
                button.handle = ^(){};
            }
            if (i==1) {
                [button setTitle:buttonTitles[i] forState:(UIControlStateNormal)];
                UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, self.ydView.buttonView.height)];
                lineView.backgroundColor = [UIColor grayColor];
                lineView.centerX = self.ydView.width/2;
                [self.ydView.buttonView addSubview:lineView];
                button.handleText = self.inputField.text;
                button.handle = confirmAction;
            }
            UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.ydView.width, 1)];
            lineview.backgroundColor = [UIColor grayColor];
            [self.ydView.buttonView addSubview:lineview];
        }
        self.ydView.alpha = 0;
        [UIView animateWithDuration:0.3 animations:^{
            self.ydView.alpha = 1;
        }];

    }
    return self;
}
#pragma mark -- 仅仅提示框
+(void)showAlertWithTitle:(NSString *)ydTitle withContent:(NSString *)ydContent{

    [[YDAlertShow shareAlert]showAlertWithTitle:ydTitle withContent:ydContent];
}
-(void)showAlertWithTitle:(NSString *)ydTitle withContent:(NSString *)ydContent{

    [self.window addSubview:self.backView];
    [self.backView addSubview:self.ydView];
    [self.ydView.titleView addSubview:self.titleLable];
    [self.ydView.contentView addSubview:self.contentLabel];
    
    self.titleLable.text = ydTitle;
    self.contentLabel.text = ydContent;
    
    self.ydView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.ydView.alpha = 1;
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.ydView.titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.ydView.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.backView removeFromSuperview];
        [self.ydView removeFromSuperview];
    });

}
+(void)showAlertWithTitle:(NSString *)ydTitle withIconStatue:(YDIconStatue)iconStaue withContent:(NSString *)ydContent{
 
    [[YDAlertShow shareAlert]showAlertWithTitle:ydTitle withIconStatue:iconStaue withContent:ydContent];
}
-(void)showAlertWithTitle:(NSString *)ydTitle withIconStatue:(YDIconStatue)iconStaue withContent:(NSString *)ydContent{

    [self.window addSubview:self.backView];
    [self.backView addSubview:self.ydView];
    [self.ydView.titleView addSubview:self.titleLable];
    [self.ydView.contentView addSubview:self.iconImage];
    [self.ydView.contentView addSubview:self.iconContent];
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",ydTitle];
    
    NSString *iconStr = [NSString stringWithFormat:@"ystatue%ld",iconStaue];
    NSLog(@"iconStr:%@",iconStr);
    self.iconImage.image = [UIImage imageNamed:iconStr];
    
    self.iconContent.text = [NSString stringWithFormat:@"%@",ydContent];
    

    self.ydView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.ydView.alpha = 1;
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.ydView.titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.ydView.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.ydView removeFromSuperview];

        [self.backView removeFromSuperview];
    });

}
#pragma mark -- 懒加载
-(UIWindow *)window{

    if (!_window) {
        _window = [UIApplication sharedApplication].keyWindow;
    }
    return _window;
    
}
-(UIView *)backView{

    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, YDMain_width, YDMain_height)];
        _backView.backgroundColor = [UIColor colorWithHexString:@"111111" alpha:0.5];
    }
    return _backView;
}
-(YDAlertView *)ydView{

    if (!_ydView) {
        _ydView = [[YDAlertView alloc]initWithFrame:CGRectMake(0, 0,YDMain_width*0.8, [YDAlertView alertHeight])];
         _ydView.center = CGPointMake(YDMain_width/2, YDMain_height/2.2);

    }
    return _ydView;
}
-(UILabel *)titleLable{

    if (!_titleLable) {
        _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.ydView.width, self.ydView.titleView.height)];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.font = [UIFont systemFontOfSize:18];
        _titleLable.textColor = [UIColor blueColor];
    }
    return _titleLable;
}
-(UILabel *)contentLabel{

    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.ydView.width/8, 0, self.ydView.width-self.ydView.width/4, self.ydView.contentView.height)];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textColor = [UIColor colorWithHexString:@"6c6c6c"];
        _contentLabel.font = [UIFont systemFontOfSize:16];
        _contentLabel.numberOfLines = 3;
        
    }
    return _contentLabel;
}
-(UIImageView *)iconImage{

    if (!_iconImage) {
        _iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, YDMain_width/8, self.contentLabel.height)];
        _iconImage.centerX = self.ydView.width/4;
        _iconImage.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconImage;
}
-(UILabel *)iconContent{

    if (!_iconContent) {
        _iconContent = [[UILabel alloc]initWithFrame:CGRectMake(self.ydView.width/2.5, _contentLabel.y, _contentLabel.width-self.ydView.width/2.5+_contentLabel.x, self.contentLabel.height)];
        _iconContent.numberOfLines = 3;
        _iconContent.font = self.contentLabel.font;
        _iconContent.textColor = self.contentLabel.textColor;
    }
    return _iconContent;
}
-(UITextField *)inputField{

    if(!_inputField){
    
        _inputField = [[UITextField alloc]initWithFrame:CGRectMake(self.contentLabel.x, 0, self.contentLabel.width, self.contentLabel.height/3)];
        _inputField.font = self.contentLabel.font;
        _inputField.textColor = self.contentLabel.textColor;
        _inputField.centerY = self.contentLabel.height/2;
        _inputField.layer.borderColor = [UIColor grayColor].CGColor;
        _inputField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, _inputField.height)];
        _inputField.leftViewMode = UITextFieldViewModeAlways;
        _inputField.layer.borderWidth = 1.0f;
        _inputField.layer.cornerRadius = 5;
        _inputField.layer.masksToBounds = YES;
    }
    return _inputField;
}

@end
